package com.mew.game.core.common.quarze;

import cn.hutool.core.util.RandomUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mew.common.enm.ZbmType;
import com.mew.game.core.common.provide.ServerRegisterFeignService;
import com.zebra.common.ZbmAreaType;
import com.zebra.common.ZbmMessage;
import com.zebra.common.concurrent.ZebraExecutorManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class MaxBonusSchedule {

    @Autowired
    private ServerRegisterFeignService serverRegisterFeignService;
    private Gson gson = (new GsonBuilder()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();

     public void startMaxBonus(){
         long delay = RandomUtil.randomInt(1200, 1800);
         long time = RandomUtil.randomInt(180, 240);
         ZebraExecutorManager.getInstance().getDefaultScheduler().scheduleWithFixedDelay(this::doMaxBonus, delay, time, TimeUnit.SECONDS, ZebraExecutorManager.getInstance().getDefaultExecutor());
     }

     private void doMaxBonus(){
         List<MaxBonusBO> list = MaxBonusManager.getInstance().getBonusCache();
         log.info("Start push max bouns list size {}", list.size());
         if (list.isEmpty()){
             return;
         }

         ZbmMessage zbmMessage = ZbmMessage.builder()
                 .zbmType(ZbmType.MaxBonus.code())
                 .zbmArea(ZbmAreaType.Global.code())
                 .msgJson(gson.toJson(list))
                 .build();
         serverRegisterFeignService.zbmCommon(zbmMessage);
         MaxBonusManager.getInstance().clear();
     }
}
